<?php
/**
 * User: Hufeng
 * Date: 2017/12/04 17:51
 * Desc: 即时通讯SERVER
 */
namespace App\Controller;
use App\Model\KeywordsModel;

class Im extends Base {

    public function chat(){
        $needParam = array(
            'openId'=>array('msg'=>'openId参数异常','type'=>'str'),
            'memo'=>array('msg'=>'memo参数异常','type'=>'str'),
        );
        $this->regArguments($needParam,$this->param);
        if($this->outData['code']){
            return $this->fjson($this->outData);
        }
        $kwModel = new KeywordsModel();
        $res = $kwModel->findData(['key_words[~]'=>$this->param['memo']]);
        if($res['id']){
            $this->outData['data'] = $res['return_info'];
        }else{
            $param = ['key'=>TULING_API_KEY,'info'=>$this->param['memo'],'userid'=>$this->param['openId']];
            $resJson = $this->httpCurl(TULING_API_URL,http_build_query($param));
            $res = json_decode($resJson,true);
            if($res['code'] == 100000){
                $this->outData['data'] = $res['text'];
            }elseif($res['code'] == 200000){
                $this->outData['data'] = $res['text'].' '.$res['url'];
            }elseif($res['code'] == 302000){
                $this->outData['data'] = $res['list'][0]['article'].' '.$res['list'][0]['detailurl'];
            }elseif($res['code'] == 308000){
                $this->outData['data'] = $res['list'][0]['name'].' '.$res['list'][0]['detailurl'];
            }else{
                $this->outData['data'] = '抱歉，暂时无法回答您的问题';
            }
        }
        return $this->fjson($this->outData);
    }
}
